#import <stdio.h>
#import <appkit/graphics.h>
#import <appkit/tiff.h>
#import "NXBitmapImageRepControl.h"
#import "tiff.h"
#import "ControlLoader.h"

void main(int argc, char *argv[]) 
{
	id			images;
	id			curImage;
	id			curConvert;
	int			x;
	char			newName[100];
	int			compress = NX_TIFF_COMPRESSION_JPEG;
	float			factor = 10.0;
	BOOL		removeIt = NO;
	
	images = [[[ControlLoader loadControl: "Bitmap"] alloc] init];

	for (x = 1; x < argc; x++) {
		if (argv[x][0] == '-') {
			switch (argv[x][1]) {
				case 'r':	removeIt = YES;
						break;
				default:	fprintf(stderr, "Unknown option: %c\n", argv[x][1]);
						break;
			}
			continue;
		}
		curImage = [images openAndReturnImage: argv[x]];
		strcpy(newName, argv[x]);
		newName[strlen(newName) - 4] = '\0'; 
		strcat(newName, ".tiff");
		[images handleLink: "tiff"];
		curConvert = [images getCurrentConverter];
		[curConvert 	setCustomParameter: 	TIFF_COMPRESS_METHOD 
					withValue: 			&compress];
		[curConvert 	setCustomParameter: 	TIFF_COMPRESS_RATIO 
					withValue: 			&factor];
		if ([images saveImage: curImage toFile: newName] && removeIt) {
			remove(argv[x]);
		}
		[curImage free];
	}

	[images free];
}
